package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Memberinfo;
import service.GradeService;
import service.MemberService;
import utils.PageBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private MemberService memberService;
    @RequestMapping("/allMember")
    public String allMember(HttpServletRequest request,Model model){
        model.addAttribute("grade",gradeService.allGrade());
        request.setAttribute("ALL_USER_LIST",memberService.allMember());
        return "product";
    }
    @RequestMapping("/find")
    public String findMemberBySearch(String mname,int typeId,HttpServletRequest request,Model model){
        List<Memberinfo> list = memberService.findMemberBySearch(mname, typeId);
        request.setAttribute("ALL_USER_LIST",list);
        model.addAttribute("grade",gradeService.allGrade());
        return "product";
    }
    @RequestMapping("/allByPage")
    public String fenye(HttpServletRequest request, Model model){
        PageBean pageBean=new PageBean();
//        接受当前的页数
        Integer index=1;
        String ind=request.getParameter("index");
        if(ind!=null&&!"".equals(ind)){
            index=Integer.parseInt(ind);
        }
//        当前页数赋值给pagebean
        pageBean.setIndex(index);
//        总条数赋值给pageBean
        pageBean.setTotalCount(memberService.selectCount());
        List<Memberinfo> list = memberService.selectPage(pageBean.getStartRow(), pageBean.getSize());
        model.addAttribute("ALL_USER_LIST",list);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("grade",gradeService.allGrade());
        return "product";
    }


    @RequestMapping("findById")
    public String findById(int id,Model model,String index){
        Memberinfo memberinfo = memberService.findById(id);
        model.addAttribute("memberinfo",memberinfo);
        model.addAttribute("index",index);
        model.addAttribute("grade",gradeService.allGrade());
        return "update";
    }


    @RequestMapping("/update")
    public String update( Memberinfo memberinfo,String index){
        int update = memberService.update(memberinfo);
        return "forward:/member/allByPage?index="+index;
    }
    @RequestMapping("/delete")
    public String deleteSingle(int id){
          memberService.deleteMwmber(id);
          return "redirect:/member/allByPage";

    }
    @RequestMapping("/deleteLot")
    public String deleteLot(String str){
        String[] split = str.split(",");
        Integer []ids=new Integer[split.length];
       for(int i=0;i<split.length;i++){
           ids[i]=Integer.parseInt(split[i]);
       }
        memberService.deltteAll(ids);
        return "redirect:/member/allByPage";

    }
}





